# Setup environment ----
library(parallel)
library(data.table)
library(bigKRLS)
source("R/functions.R")
results_path <- "results/replication/"
load(paste0(results_path, "house_analysis_data.RData"))
load(paste0(results_path, "senate_analysis_data.RData"))

# House KRLS ----
house_krls_model <- bigKRLS(X = hou_X, y = hou_y, noisy = TRUE)
house_krls_results <- as.data.table(cbind(hou_X, house_krls_model$derivatives))
setnames(house_krls_results, c(colnames(hou_X), paste0("d_", colnames(hou_X))))
house_krls_cv <- cv_bigKRLS(hou_folds, hou_X, hou_y)
save.bigKRLS(house_krls_model,
  model_subfolder_name = paste0(results_path, "house_krls_model"))
save(house_krls_results,
  file = paste0(results_path, "house_krls_results.RData"))
save(house_krls_cv, file = paste0(results_path, "house_krls_cv.RData"))

# Senate KRLS ----
senate_krls_model <- bigKRLS(X = sen_X, y = sen_y, noisy = TRUE)
senate_krls_results <- as.data.table(
  cbind(sen_X, senate_krls_model$derivatives))
setnames(senate_krls_results,
  c(colnames(sen_X), paste0("d_", colnames(sen_X))))
senate_krls_cv <- cv_bigKRLS(sen_folds, sen_X, sen_y)
save.bigKRLS(senate_krls_model,
  model_subfolder_name = paste0(results_path, "senate_krls_model"))
save(senate_krls_results,
  file = paste0(results_path, "senate_krls_results.RData"))
save(senate_krls_cv, file = paste0(results_path, "senate_krls_cv.RData"))

